package com.chazz.dao.impl;

import cn.hutool.db.Db;
import cn.hutool.db.Entity;
import com.chazz.Do.KeyValue;
import com.chazz.dao.SkuDao;
import com.chazz.entity.Attributekey;
import com.chazz.entity.Brand;
import com.chazz.entity.Discount;
import com.chazz.entity.Goods;
import com.chazz.entity.Sku;
import com.chazz.entity.Value;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import com.chazz.utils.DBUtils;
import java.sql.ResultSet;
/**
 * 描述:商品通用信息查询DAO实现
 *
 * @author Chazz
 * @create 2020-09-24 19:22
 */
public class SkuDaoImpl implements SkuDao {
    /**
     * description: selectGoods 通过商品id查询商品简单信息 <br>
     * version: 1.0 <br>
     * date: 2020/9/25 9:46 <br>
     * author: Chazz <br>
     * @param goodsId
     * @return com.chazz.entity.Goods
     */
    @Override
    public Goods selectGoods(Integer goodsId) {
        Goods goods = new Goods();
        try {
            List<Goods> goodsList = Db.use()
                .query("select * from goods where goodsid = ?", Goods.class, goodsId);
            goods = goodsList.get(0);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return goods;
    }

    /**
     * description: selectSku 通过商品id查询商品通用信息 <br>
     * version: 1.0 <br>
     * date: 2020/9/24 19:26 <br>
     * author: Chazz <br>
     * @param goodsId 商品id
     * @return com.chazz.entity.Sku 商品通用信息数据对象
     */ 
    @Override
    public Sku selectSku(Integer goodsId) {
        List<Sku> skus = new ArrayList<>();
        try {
            skus = Db.use()
                .query("select * from sku where goodsid = ?", Sku.class, goodsId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return skus.get(0);

    }

    /**
     * description: seclectBrand 通过商品品牌id查询品牌信息 <br>
     * version: 1.0 <br>
     * date: 2020/9/24 19:31 <br>
     * author: Chazz <br>
     * @param brandId 品牌id
     * @return com.chazz.entity.Brand 品牌信息对象
     */
    @Override
    public Brand seclectBrand(Integer brandId) {
        List<Brand> brands = new ArrayList<> ();
        try {
            brands = Db.use()
                .query("select * from brand where brandid = ?", Brand.class, brandId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return brands.get(0);
    }
    /**
     * description: attributes  通过类别id查询改类别拥有的所有属性 <br>
     * version: 1.0 <br>
     * date: 2020/9/24 20:12 <br>
     * author: Chazz <br>
     * @param categoryId 类别id
     * @return java.util.Map<java.lang.String,java.lang.String> 属性map集合
     */
    @Override
    public List<KeyValue> attributes(Integer categoryId) {
        List<KeyValue> map = new ArrayList<> ();
        List<Attributekey> attributeKeys;
        try {
            attributeKeys = Db.use().query("select * from attributekey where categoryId =?", Attributekey.class, categoryId);
            System.out.println(attributeKeys.size());
            for (Attributekey attributekey : attributeKeys) {
                List<String> values = new ArrayList<> ();
                List<Value> valueList = Db.use().query("select * from value where attributeid =?", Value.class, attributekey.getAttributeid());
                for (Value value : valueList) {
                    values.add(value.getAttributevalue());
                }
                KeyValue keyValue = new KeyValue(attributekey.getAttributename(),values);
                map.add(keyValue);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return map;
    }

    @Override
    public Discount selectDiscount(Integer categoryId) {
        List<Discount> discounts = null;
        try {
            discounts = Db.use()
                .query("select * from discount  where categoryid = ?", Discount.class, categoryId);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return discounts.get(0);

        /**
         * @param goodsID
         * @return
         * @author Woo
         */
    }
    @Override
    public Sku findSku(int goodsID) {
        Sku sku = null;
        try {
            sku = (Sku) Db.use().findAll(Entity.create("sku").set("goodsid", "goodID"));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return sku;
    }
}
